#include "sgame.h"

int32 sgame_timer;
extern int main();

//取大值
int maxInt(int a,int b)
{
 return a>b?a:b;
}

//取小值
int minInt(int a,int b)
{
 return a<b?a:b;
}

//定时器回调
//运行并绘制当前房间
void gameRun(int32 data)
{
 cls(0xff,0xff,0xff);
 if(_room==NULL)
 dtext("sgame load error",
 0,0,255,0,0,FALSE,1);
 sRunCurRoom();
 sDrawCurRoom();
 ref(0,0,SW,SH);
}

//入口函数
int init()
{
 cls(255,255,255);
 setscrsize(480,480*SH/SW);
 switchPath("c");
 
 //初始化定时器
 sgame_timer=
 getRes("sgame_timer",tTim)->id;
 timerstart(sgame_timer,30,0,
 gameRun,TRUE);
 
 //调用main
 int load = main();
 if(load==FALSE)
 {
  printf("sgame>function main return error!");
  exit();
 }
 //刷新屏幕
 ref(0,0,SW,SH);
 return 0;
}

//event函数
int event(int type,int p1,int p2)
{
 if(_room!=NULL)
 {
  sCallObjSenEve(_room->sen,
  type,p1,p2);
  if(_room->event!=NULL)
  	 _room->event(type,p1,p2);
 }
 else if(_BACK==p1)
 {
  exit();
 }
 return 0;
}

//应用暂停
int pause()
{
 return 0;
}

//应用恢复
int resume()
{
 return 0;
}

//退出函数
int exitApp()
{
 freeRes();
 return 0;
}